import axios from 'axios'

// const tokenKey = 'x-auth-token'

const URL = {
    production: 'http://192.168.78.49:9000/cqp-litapp-common/litapp/security/loginByTicket',
    development: 'http://192.168.78.49:9000/cqp-litapp-common/litapp/security/loginByTicket'
}
import baseUrl,{tokenKey} from '@apis/javaBaseUrl.js'

const getTicketType = (value = 0) => {
    if (process.env.NODE_ENV === 'production') {
        return value
    }

    return 0
}

export const getToken = (ticket = '', ticketType) => {
    return axios.create({
        headers: {
            'Content-Type': 'application/json;charset=UTF-8'
        },
        timeout: 5000
    }).post(baseUrl[process.env.NODE_ENV]+'login/loginByTicket', {
        ticket,
        ticketType: getTicketType(ticketType)
    }).then(res => {
        if (res.status === 200 && res.headers[tokenKey]) {
            return {
                success: true,
                token: res.headers[tokenKey],
                data: res.data.data
            }
        }

        throw {}

    }).catch(err => ({ ...err.response.data || '获取权限出错', success: false }))
}